package com.crazyhat.crazyhat.service.impl;

import static com.crazyhat.crazyhat.attribute.ClubStatusEnum.Open;
import static com.crazyhat.crazyhat.attribute.ClubStatusEnum.Rejected;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazyhat.crazyhat.attribute.UserTypeEnum;
import com.crazyhat.crazyhat.entity.AdminMessage;
import com.crazyhat.crazyhat.entity.Club;
import com.crazyhat.crazyhat.mapper.AdminMessageMapper;
import com.crazyhat.crazyhat.mapper.ClubMapper;
import com.crazyhat.crazyhat.service.IAdminMessageService;
import com.crazyhat.crazyhat.service.IMessageService;
import com.crazyhat.crazyhat.service.IUserService;
import com.crazyhat.crazyhat.utils.ThreadLocalUtil;

/**
 * <p>
 * 给系统管理员的消息 服务实现类
 * </p>
 *
 * @author crazyhat
 * @since 2024-07-05
 */
@Service
public class AdminMessageServiceImpl extends ServiceImpl<AdminMessageMapper, AdminMessage> implements IAdminMessageService {

    @Autowired
    private AdminMessageMapper adminMessageMapper;

    @Autowired
    private ClubMapper clubMapper;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IUserService userService;
    @Override
    public Page<AdminMessage> checkMassages(int pageNum, int pageSize){
        //检查是不是系统管理员
        if(!isManager()){
            Page<AdminMessage> emptyPage = new Page<>(pageNum, pageSize);
            emptyPage.setTotal(0);
            emptyPage.setRecords(new ArrayList<>());
            return emptyPage;
        }
        QueryWrapper<AdminMessage> queryWrapper = new QueryWrapper<>();
        // 创建分页对象
        Page<AdminMessage> page = new Page<>(pageNum, pageSize);
        // 分页查询
        return adminMessageMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean approveRequest(Integer messageId, boolean approve) {
        AdminMessage message = adminMessageMapper.selectById(messageId);
        if (message == null) {
            return false;
        }
        Club club = clubMapper.selectById(message.getClubId());
        if (club == null) {
            return false;
        }

        if(!isManager()){
            return false;
        }

        if (approve) {
            club.setClubStatus(Open);
        } else {
            club.setClubStatus(Rejected);
        }
        club.setUpdateTime(LocalDateTime.now());
        int updateResult = clubMapper.updateById(club);

        // 发送消息给申请人
        String content;

        content="您的“%s”的社团创建申请已被".formatted(club.getClubName(),approve?"通过":"拒绝");

        messageService.sendMessageToStudent(club.getLeaderId(), club.getClubId(), content);
        // 删除或标记处理后的消息
        adminMessageMapper.deleteById(messageId);
        return updateResult == 1;
    }

    public boolean isManager(){
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer studentId=(Integer)map.get("studentId");
        UserTypeEnum type=userService.selectById(studentId).getUserType();
        return type.getValue().equals("系统管理员");
    }

    @Override
    public AdminMessage getMessage(int clubId){
        //检查是不是系统管理员
        if(!isManager()){
            return null;
        }
        QueryWrapper<AdminMessage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("club_id",clubId);
        AdminMessage adminMessage=adminMessageMapper.selectOne(queryWrapper);
        // 分页查询
        return adminMessage;
    }
}
